package backpage.service.Impl;

import backpage.entity.po.CompoffApply;
import backpage.entity.po.OvertimeApply;
import backpage.entity.vo.OvertimeApplyVo;
import backpage.mapper.OvertimeApplyMapper;
import backpage.service.IOvertimeApplyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 加班信息 服务实现类
 * </p>
 *
 * @author 刘仁杰
 * @since 2025-04-19
 */
@Service
@Slf4j
public class OvertimeApplyServiceImpl extends ServiceImpl<OvertimeApplyMapper, OvertimeApply> implements IOvertimeApplyService {

    // 保存申请
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOvertimeApply(OvertimeApply overtimeApply) {
        // TODO 如果申请的 employId 和当前请求的 employId 不同则返回
        Date startTime = overtimeApply.getStartTime();
        Date endTime = overtimeApply.getEndTime();
        if (startTime != null && endTime != null && startTime.after(endTime)) {
            throw new RuntimeException("开始时间不能晚于结束时间");
        }
        this.save(overtimeApply);
    }

    // 提交申请
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOvertimeApply(OvertimeApply overtimeApply) {
        saveOrUpdate(overtimeApply);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        OvertimeApply overtimeApply = baseMapper.selectById(id);
        if (overtimeApply == null) {
            return false;
        }
        this.removeById(id);
        return true;
    }

    @Override
    public PageInfo<OvertimeApplyVo> getListByEmployIdAndParam(Integer employId, Date startTime, Date endTime, Integer state,Integer type, Integer approveId, int pageNum, int pageSize) {
//        LambdaQueryWrapper<OvertimeApply> lambdaQuery = new LambdaQueryWrapper<>();
//
//        if (employId != null) {
//            lambdaQuery.eq(OvertimeApply::getEmployeeId, employId);
//        }
//
//        if (approveId != null) {
//            lambdaQuery.and(wrapper -> wrapper
//                    .ne(OvertimeApply::getState, 0)
//                    .eq(OvertimeApply::getApproverId, approveId)
//            );
//        }
//
//        if (startTime != null) {
//            lambdaQuery.ge(OvertimeApply::getStartTime, startTime);
//        }
//
//        if (endTime != null) {
//            lambdaQuery.le(OvertimeApply::getEndTime, endTime);
//        }
//
//        if (state != null && state != 0) {
//            lambdaQuery.eq(OvertimeApply::getState, state);
//        }

        PageHelper.startPage(pageNum, pageSize);
        List<OvertimeApplyVo> listByEmployIdAndParam = this.baseMapper.getListByEmployIdAndParam(employId, startTime, endTime, state,type, approveId);
        log.info(listByEmployIdAndParam.toString());
        return new PageInfo<>(listByEmployIdAndParam);
    }


    @Override
    public PageInfo<OvertimeApply> getListByApproverIdAndParam(Integer approverId, Date startTime, Date endTime, Integer state, int pageNum, int pageSize) {
        LambdaQueryWrapper<OvertimeApply> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(OvertimeApply::getApproverId, approverId);

        // 处理开始时间和结束时间为空的情况
        if (startTime != null) {
            lambdaQuery.ge(OvertimeApply::getStartTime, startTime);
        }
        if (endTime != null) {
            lambdaQuery.le(OvertimeApply::getEndTime, endTime);
        }
        if (state != null) {
            lambdaQuery.eq(OvertimeApply::getState, state);
        }

        // 使用 PageHelper 开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<OvertimeApply> list = this.list(lambdaQuery);
        return new PageInfo<>(list);
    }

    @Override
    public boolean appoverOvertimeApply(OvertimeApply overtimeApply) {
        if (overtimeApply.getId() != null) {
            overtimeApply.setState(2); // 假设 2 为审批通过状态
            return this.updateById(overtimeApply);
        }
        return false;
    }

    @Override
    public void quickApproveOvertimeApply(Long id, Integer state) {
        // 参数校验
        if (id == null || state == null) {
            throw new IllegalArgumentException("申请ID和审批状态不能为空");
        }

        // 创建UpdateWrapper进行部分更新
        UpdateWrapper<OvertimeApply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id).set("state", state);

        // 执行更新操作
        boolean success = this.update(null, updateWrapper);
        // 检查更新结果
        if (!success) {
//            log.warn("加班申请快速审批失败，ID: {}", id);
            throw new RuntimeException("申请不存在或更新失败");
        }
    }

    public List<OvertimeApply> getApprovedOvertimeApplies(Date startDate, Date endDate) {
        // 构建查询条件
        LambdaQueryWrapper<OvertimeApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(OvertimeApply::getEndTime, startDate)
                .lt(OvertimeApply::getEndTime, endDate)
                .eq(OvertimeApply::getState, 2); // 假设审批通过状态为 2

        // 执行查询
        return this.list(queryWrapper);
    }
}
